﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace OTPlus
{
    public partial class Primary : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //http://code.google.com/apis/accounts/docs/OAuth2WebServer.html
            //***Forming The URL ******

            string clientid = System.Configuration.ConfigurationManager.AppSettings["gplusclientid"];
            string clientsecret = System.Configuration.ConfigurationManager.AppSettings["gplusclientsecret"];
            string clientredirecturi = System.Configuration.ConfigurationManager.AppSettings["gplusclientredirecturi"];

            String navurl = "scope=" + HttpUtility.UrlEncode("https://www.googleapis.com/auth/plus.me") +
            "&approval_prompt=force" +
            "&redirect_uri=" + HttpUtility.UrlEncode(clientredirecturi) +
            "&response_type=code" +
            "&access_type=offline" +
            "&client_id=" + clientid;

            navurl = "https://accounts.google.com/o/oauth2/auth?" + navurl;

            this.linkLogin.NavigateUrl = navurl;

            if (Session["access_token"] == null)
            {
                //this.linkLogin.NavigateUrl = navurl;
                Response.Redirect(this.linkLogin.NavigateUrl);

            }
            else
            { this.linkLogin.Visible = false; this.linkHome.Visible = true; }

            this.panelDiagnostics.Visible = true;
            this.labelDiagnosticsAccessToken.Text = Session["access_token"].ToString();
            
            Core.Storage.ActivityFetchCache afc = new Core.Storage.ActivityFetchCache();
            
            
            //let's grab some records, eh?
            afc.RefreshIncremenetalCacheData(Session["access_token"].ToString());

            if (afc.AccessCacheData().Count > 0)
            {
                var max = afc.AccessCacheData().Max(x => x.LastChecked);
                this.lablDiagnosticsActivityFetchCache.Text = afc.AccessCacheData().Count.ToString() + " most recent:" + max.ToString();
            }
            else
            {
                this.lablDiagnosticsActivityFetchCache.Text = "0";
            }

            if (Core.Storage.ThreadSummaryCache.Instance.AccessCacheData().Count > 0)
            {
                var max = Core.Storage.ThreadSummaryCache.Instance.AccessCacheData().Count().ToString();
                this.lablDiagnosticsThreadSummaryCache.Text = max.ToString();                
            }


            


        }
    }
}